package com.ejia.server;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.ejia.dao.BaseDao;
import com.ejia.pojo.fytype;

public class MtypeService {

	public static ArrayList<fytype> find() {
		String sql="select fyid,fyname,fyprice,unit,Fydetail from tb_fytype";
		BaseDao baseDao=new BaseDao();
		ResultSet rSet=baseDao.query(sql);
		ArrayList<fytype> fytypes=new ArrayList<>();
		fytype fytype=null;
		try {
			while (rSet.next()) {
				fytype= new fytype();
				fytype.setFyid(rSet.getInt("fyid"));
				fytype.setFyname(rSet.getString("fyname"));
				fytype.setFyprice(rSet.getDouble("fyprice"));
				fytype.setUnit(rSet.getString("unit"));
				fytype.setFydetail(rSet.getString("Fydetail"));
				fytypes.add(fytype);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return fytypes;
	}
	
	public static ArrayList<fytype> findid(String fyname) {
		String sql="select fyid,fyprice,unit from tb_fytype where fyname=?";
		BaseDao baseDao=new BaseDao();
		ResultSet rSet=baseDao.query(sql,fyname);
		ArrayList<fytype> fytypes=new ArrayList<>();
		fytype fytype=null;
		try {
			while (rSet.next()) {
				fytype= new fytype();
				fytype.setFyid(rSet.getInt("fyid"));
				fytype.setFyprice(rSet.getDouble("fyprice"));
				fytype.setUnit(rSet.getString("unit"));
				fytypes.add(fytype);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return fytypes;
	}
	
	
	
	public static boolean add(String name,double price,String unit,String detail) {
		String sql="insert into tb_fytype(fyname,fyprice,unit,fydetail) values(?,?,?,?)";
		return new BaseDao().execute(sql, name,price,unit,detail);
	}
	
	public static boolean delete(int id) {
		String sql="delete from tb_fytype where fyid=?";
		return new BaseDao().execute(sql,id);
	}
	
	public static Boolean update(String name,double price,String unit,String detail,int id) {
		String sql="update tb_fytype set fyname=?,fyprice=?,unit=?,fydetail=? where fyid=?";
		return new BaseDao().execute(sql, name,price,unit,detail,id);
	}
	
	
	
}
